//
//  CHSWindowViewController.m
//  ZXCategories_Example
//
//  Created by zhuxm on 2024/8/9.
//  Copyright © 2024 海参. All rights reserved.
//

#import "CHSWindowViewController.h"
#import <Masonry/Masonry.h>
#import <ZXCategoriesUtil/ZXCategoriesUtil.h>
#import <ZXMBProgressHUD/ZXMBProgressHUD.h>

@interface CHSWindowViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (atomic, strong) NSMutableArray *dataMArray;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation CHSWindowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
    [self addMas];
    [self setData];
}


- (void)setUI{
    self.view.backgroundColor = [UIColor zx_colorWithHexString:@"#F5F5F5"];
    self.navigationItem.title = self.navTitle;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"toast" style:UIBarButtonItemStyleDone target:self action:@selector(rightItemAction)];
    [self.view addSubview:self.tableView];
}

- (void)rightItemAction{
    [MBProgressHUD zx_showSuccess:@"发货成功"];
}

- (void)addMas{
    
    CGFloat top = UIApplication.zx_safeAreaNavBarHeight;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {      make.left.mas_equalTo(self.view.mas_left).with.offset(15);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.view.mas_top).with.offset(top);
        make.bottom.mas_equalTo(self.view.mas_bottom);
    }];
    
  
}

#pragma mark - Data

- (void)setData
{
    NSArray *arr = @[
    @{@"windows":@"windows"},
    @{@"mainWindow":@"mainWindow"},
    @{@"keyWindow":@"keyWindow"},
    @{@"remoteKeyboardWindow":@"remoteKeyboardWindow"},
    @{@"zx_frontWindow":@"zx_frontWindow"},
    @{@"currentController":@"获取当前控制器"},

    ];
    self.dataMArray = [NSMutableArray arrayWithArray:arr];
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        UITableView *view = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        view.backgroundColor = [UIColor redColor];
        view.delegate = self;
        view.dataSource = self;
        view.showsVerticalScrollIndicator = NO;
//        [view registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
        view.estimatedRowHeight = UITableViewAutomaticDimension;
        view.rowHeight = UITableViewAutomaticDimension;
        if (@available(iOS 15.0, *)) {
            view.sectionHeaderTopPadding = 0;
        } else {
            // Fallback on earlier versions
        }
        _tableView = view;
    }
    return _tableView;
}

#pragma mark - TableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataMArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
        cell.backgroundColor = [UIColor greenColor];
        cell.textLabel.numberOfLines = 0;
        cell.detailTextLabel.numberOfLines = 0;
    }
    NSDictionary *dic = [self.dataMArray objectAtIndex:indexPath.row];
    NSString *key = dic.allKeys.firstObject;
    NSString *value = dic.allValues.firstObject;
    cell.textLabel.text = value;
    NSString *description;
    if ([key isEqualToString:@"windows"]) {
        NSArray *windows = UIWindow.zx_windows;
        description = windows.description;
    }
    else if ([key isEqualToString:@"mainWindow"]) {
        UIWindow *mainWindow = UIWindow.zx_mainWindow;
        description =mainWindow.description;
    }
    else if ([key isEqualToString:@"keyWindow"]) {
        UIWindow *keyWindow = UIWindow.zx_keyWindow;
        description = keyWindow.description;
    }
    else if ([key isEqualToString:@"remoteKeyboardWindow"]) {
        UIWindow *window = UIWindow.zx_remoteKeyboardWindow;
        description = window.description;
    }
//    else if ([key isEqualToString:@"zx_frontVisibleWindow"]) {
//        UIWindow *window = UIWindow.zx_frontVisibleWindow;
//        description = window.description;
//    }
    else if ([key isEqualToString:@"zx_frontWindow"]) {
        UIWindow *window = UIWindow.zx_frontWindow;
        description = window.description;
    }
    else if ([key isEqualToString:@"currentController"]) {
        UIViewController *obj = UIWindow.zx_currentViewController;
        description = [NSString stringWithFormat:@"CHSWindowViewController:%@",obj.description];
    }
    cell.detailTextLabel.text = description;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return UITableViewAutomaticDimension;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UITextField *tx = [[UITextField alloc] init];
    tx.delegate = self;
    tx.placeholder = @"请输入";
    tx.frame = CGRectMake(0, 0, CGRectGetWidth(tableView.frame),0);
    tx.backgroundColor = [UIColor whiteColor];
    return tx;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    [self.tableView reloadData];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
